package com.vero.appointment.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.vero.common.core.utils.StringUtils;
import com.vero.system.api.RemoteInventoryService;
import com.vero.system.api.RemoteNotificationService;
import com.vero.system.api.domain.Vaccine;
import com.vero.system.api.model.EmailVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.vero.common.log.annotation.Log;
import com.vero.common.log.enums.BusinessType;
import com.vero.common.security.annotation.RequiresPermissions;
import com.vero.system.api.domain.AppointmentVaccination;
import com.vero.appointment.service.IAppointmentVaccinationService;
import com.vero.common.core.web.controller.BaseController;
import com.vero.common.core.web.domain.AjaxResult;
import com.vero.common.core.utils.poi.ExcelUtil;
import com.vero.common.core.web.page.TableDataInfo;

/**
 * 预约接种记录Controller
 * 
 * @author ruoyi
 * @date 2025-03-06
 */
@RestController
@RequestMapping("/appointmentVaccination")
public class AppointmentVaccinationController extends BaseController
{
    @Autowired
    private IAppointmentVaccinationService appointmentVaccinationService;

    /**
     * 查询预约接种记录列表
     */
    @RequiresPermissions("appointment:appointmentVaccination:list")
    @GetMapping("/list")
    public TableDataInfo list(AppointmentVaccination appointmentVaccination)
    {
        startPage();
        List<AppointmentVaccination> list = appointmentVaccinationService.selectAppointmentVaccinationList(appointmentVaccination);
        return getDataTable(list);
    }

    /**
     * 导出预约接种记录列表
     */
    @RequiresPermissions("appointment:appointmentVaccination:export")
    @Log(title = "预约接种记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppointmentVaccination appointmentVaccination)
    {
        List<AppointmentVaccination> list = appointmentVaccinationService.selectAppointmentVaccinationList(appointmentVaccination);
        ExcelUtil<AppointmentVaccination> util = new ExcelUtil<AppointmentVaccination>(AppointmentVaccination.class);
        util.exportExcel(response, list, "预约接种记录数据");
    }

    /**
     * 获取预约接种记录详细信息
     */
    @RequiresPermissions("appointment:appointmentVaccination:query")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId)
    {
        return success(appointmentVaccinationService.selectAppointmentVaccinationByRecordId(recordId));
    }
    /**
     * 取消接种
     */
    @Autowired
    private RemoteNotificationService remoteNotificationService;
    @Autowired
    private RemoteInventoryService remoteInventoryService;
    @RequiresPermissions("appointment:appointmentVaccination:add")
    @PutMapping(value = "/cancel/{recordId}")
    public AjaxResult cancel(@PathVariable("recordId") Long recordId)
    {
        AppointmentVaccination appointmentVaccination = appointmentVaccinationService.selectAppointmentVaccinationByRecordId(recordId);
        //发邮件
        if (StringUtils.isNotBlank(appointmentVaccination.getUserEmail())) {
            EmailVO emailVO = new EmailVO();
            emailVO.setToEmail(appointmentVaccination.getUserEmail());
            emailVO.setSubject("您的疫苗接种预约已取消");
            Date appointmentTime = appointmentVaccination.getAppointmentTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String strTime = sdf.format(appointmentTime);
            String location = appointmentVaccination.getLocation();
            emailVO.setContent("尊敬的用户，\n" +
                    "您已成功取消疫苗接种预约。如果是误操作，您可以重新预约，以确保按时接种疫苗。\n" +
                    "如果您有任何疑问，请随时联系我们。\n" +
                    "感谢您的关注，愿您健康平安！\n" +
                    "预约时间:"+strTime+"\n" +
                            "预约地点:"+location);
            remoteNotificationService.send(emailVO);
        }
        Long vaccineId = appointmentVaccination.getVaccineId();
        Vaccine vaccine = new Vaccine();
        vaccine.setVaccineId(vaccineId);
        vaccine.setQuantity(1L);
        //减库存
        remoteInventoryService.updateVaccineStock(vaccine);
        AppointmentVaccination appointmentVaccination1 = new AppointmentVaccination();
        appointmentVaccination1.setStatus("2");
        appointmentVaccination1.setRecordId(recordId);
        return success(appointmentVaccinationService.updateAppointmentVaccination(appointmentVaccination1));
    }
    /**
     * 新增预约接种记录
     */
    @RequiresPermissions("appointment:appointmentVaccination:add")
    @Log(title = "预约接种记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppointmentVaccination appointmentVaccination)
    {
        return toAjax(appointmentVaccinationService.insertAppointmentVaccination(appointmentVaccination));
    }

    /**
     * 修改预约接种记录
     */
    @RequiresPermissions("appointment:appointmentVaccination:edit")
    @Log(title = "预约接种记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppointmentVaccination appointmentVaccination)
    {
        return toAjax(appointmentVaccinationService.updateAppointmentVaccination(appointmentVaccination));
    }
    /**
     * 修改预约接种记录
     */
    @RequiresPermissions("appointment:appointmentVaccination:remove")
    @Log(title = "预约接种记录", businessType = BusinessType.UPDATE)
    @PutMapping("/vaccinate")
    public AjaxResult vaccinate(@RequestBody AppointmentVaccination appointmentVaccination)
    {
        appointmentVaccination.setStatus("1");

        AppointmentVaccination appointmentVacci = appointmentVaccinationService.selectAppointmentVaccinationByRecordId(appointmentVaccination.getRecordId());
        //发邮件
        if (StringUtils.isNotBlank(appointmentVacci.getUserEmail())) {
            EmailVO emailVO = new EmailVO();
            emailVO.setToEmail(appointmentVacci.getUserEmail());
            emailVO.setSubject("您的疫苗已接种完成");
            Date appointmentTime = appointmentVaccination.getVaccinationDate();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String strTime = sdf.format(appointmentTime);
            String location = appointmentVaccination.getLocation();
            emailVO.setContent("尊敬的用户，\n" +
                    "您的疫苗已接种完成。请您在观察室停留30分钟,确认无不良反应后再离开!!!\n" +
                    "如果您有任何疑问，请随时联系我们。\n" +
                    "感谢您的关注，愿您健康平安！\n" +
                    "接种日期:"+strTime+"\n" +
                            "预约地点:"+location);
            remoteNotificationService.send(emailVO);
        }






        return toAjax(appointmentVaccinationService.updateAppointmentVaccination(appointmentVaccination));
    }

    /**
     * 删除预约接种记录
     */
    @RequiresPermissions("appointment:appointmentVaccination:remove")
    @Log(title = "预约接种记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds)
    {
        return toAjax(appointmentVaccinationService.deleteAppointmentVaccinationByRecordIds(recordIds));
    }
}
